<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Model;

class BaseModel extends Model
{
    public static function getPaginate($where,$order = ['id', 'desc'], $limit = 10)
    {
        list($order, $sort) = $order;
        return self::where($where)->paginate($limit)->toArray();
    }

    public static function jsonDecodeImg($img)
    {
        if (is_array($img)) {
            foreach($img as &$val){
                if (strpos($val,'http') === false) {
                    $val = env('APP_URL').'/uploads/'.$val;
                }
            }
            return $img;
        }
        if (!empty($img) && is_string($img)) {
            return strpos($img,'http') === false ? env('APP_URL').'/uploads/'.$img: $img;
        }
        return $img;
    }

    public static function createModel($model, $input)
    {
        if (is_object($model)) {
            foreach($input as $key=>$val){
                if(!empty($input[$key])){
                    $model->$key = $input[$key];
                }
            }
            $model->save();
            return $model;
        }
        return null;
    }

    public static function getDataByWhere($where)
    {
        return self::where($where)->first();
    }

    //环比计算
    public static function ringRate($curr, $last){
        if(empty($curr) || empty($last)){
            return 0;
        }
        $c = $curr-$last;
        $rate = $c/$last*100;
        $rate = floor($rate);
        return $rate;
    }

    //100%占比
    public static function roundRingRate($curr, $last){
        if(empty($curr) || empty($last)){
            return 0;
        }
        $rate = $curr/$last*100;
        $rate = round($rate);
        return $rate;
    }

    //双柱线图数据结构
    public static function twoLine($now_days, $tongbi_array, $sum_array,$string1 = '同比',$string2 = '业绩量')
    {
        $new_data = [
            'area' => [
                'categories' =>$now_days,
                'series' =>[
                    [
                        'name' => $string1,
                        'data' => $tongbi_array,
                        'color'=> '#F83600'
                    ],
                    [
                        'name' => $string2,
                        'data' => $sum_array,
                        'color'=> '#F9D423'
                    ],
                ],
            ],
        ];
        return $new_data;
    }

    public static function oneLine($now_days, $sum_array, $string = '人数')
    {
        $new_data = [
            'area' => [
                'categories' => $now_days,
                'series' =>[
                    [
                        'name' => $string,
                        'data' => $sum_array,
                        'color'=> '#F83600'
                    ]
                ],
            ],
        ];
        return $new_data;
    }

    public static function zengzhangRate($now, $last)
    {
        if (empty($now) || empty($last)) {
            return 0;
        }
        $num = $now-$last;
        $rate = $num/$last*100;
        return round($rate);
    }

    public static function tiaoxing($num1, $num2, $num3, $color = [])
    {
        if(empty($color)){
            $color = [
                '#F8C91F',
                '#47C0F8',
                '#F84611',
            ];
        }
        return [
            ['num'=> $num1, 'color' => $color[0]],
            ['num'=> $num2, 'color' => $color[1]],
            ['num'=> $num3, 'color' => $color[2]],
        ];
    }

    public static function lineDays($date, $type)
    {
        $now_days = [];
        if($type == 1){
            $now_days[] = $date;
        }
        if($type == 2){
            $now_days = get_week_days($date);
        }
        if($type == 3){
            $now_days = get_month_days($date);
        }
        return $now_days;
    }

}